
#include "EGL_Base.h"


/////////////////////////////////////
////          Struct part        ////
/////////////////////////////////////


EGL_Base_Conf::EGL_Base_Conf() : Draw_Base_Conf()
{
    m_vEglDisplay = EGL_NO_DISPLAY;
    m_vEglContext = EGL_NO_CONTEXT;
    m_vEglSurface = EGL_NO_SURFACE;
}



/////////////////////////////////////
////          Class part         ////
/////////////////////////////////////


EGL_Base::EGL_Base() : Draw_Base()
{
    m_sEGL_Base_Conf = new EGL_Base_Conf();
}

/**
 *
 */
void EGL_Base::initDisplay()
{
    m_sEGL_Base_Conf->m_vEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    m_sEGL_Base_Conf->m_bSuccess = eglInitialize(m_sEGL_Base_Conf->m_vEglDisplay, &(m_sEGL_Base_Conf->m_nMajorVersion),  &(m_sEGL_Base_Conf->m_nMinorVersion));

    if(m_sEGL_Base_Conf->m_bSuccess != EGL_FALSE)
    {
        m_sEGL_Base_Conf->m_bSuccess = eglGetConfigs(m_sEGL_Base_Conf->m_vEglDisplay, NULL, 0, &(m_sEGL_Base_Conf->m_nNumConfigs));
    }

    const EGLint ConfigAttribute[] =
    {
        EGL_RED_SIZE,       5,
        EGL_GREEN_SIZE,     6,
        EGL_BLUE_SIZE,      5,
        EGL_DEPTH_SIZE,     16,
        EGL_ALPHA_SIZE,     EGL_DONT_CARE,
        EGL_STENCIL_SIZE,   EGL_DONT_CARE,
        EGL_SURFACE_TYPE,   EGL_WINDOW_BIT,
	//EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,  
	
        EGL_NONE
    };
    // malloc config's space
    m_sEGL_Base_Conf->m_vEglConfig = malloc(m_sEGL_Base_Conf->m_nNumConfigs * sizeof(EGLConfig));
    // choose my config
    if (m_sEGL_Base_Conf->m_bSuccess != EGL_FALSE)
    {
        m_sEGL_Base_Conf->m_bSuccess = eglChooseConfig(m_sEGL_Base_Conf->m_vEglDisplay, ConfigAttribute, &(m_sEGL_Base_Conf->m_vEglConfig), 1, &m_sEGL_Base_Conf->m_nNumConfigs);
    }
    // create my context
    if (m_sEGL_Base_Conf->m_bSuccess != EGL_FALSE)
    {
        m_sEGL_Base_Conf->m_vEglContext = eglCreateContext(m_sEGL_Base_Conf->m_vEglDisplay, m_sEGL_Base_Conf->m_vEglConfig, EGL_NO_CONTEXT, NULL);
        if (m_sEGL_Base_Conf->m_vEglConfig == EGL_NO_CONTEXT)
        {
            m_sEGL_Base_Conf->m_bSuccess = EGL_FALSE;
        }
    }

    EGLint vid;
    // check the result config
    eglGetConfigAttrib(m_sEGL_Base_Conf->m_vEglDisplay, m_sEGL_Base_Conf->m_vEglConfig, EGL_SURFACE_TYPE, &vid);

    LOGI("majorV %d, minorV %d confNum %d attrsss %d\n", m_sEGL_Base_Conf->m_nMajorVersion, m_sEGL_Base_Conf->m_nMinorVersion, m_sEGL_Base_Conf->m_nNumConfigs, vid);

}

/**
 *
 */
void EGL_Base::initWindowSurface(struct android_app* a_sApp)
{
    //LOGI("The Init Window: width %d height %d \n", a_sApp->window->width, a_sApp->window->height);
    
    if (m_sEGL_Base_Conf->m_bSuccess != EGL_FALSE)
    {
        m_sEGL_Base_Conf->m_vEglSurface = eglCreateWindowSurface(m_sEGL_Base_Conf->m_vEglDisplay, m_sEGL_Base_Conf->m_vEglConfig, a_sApp->window, NULL);
        if (m_sEGL_Base_Conf->m_vEglSurface == EGL_NO_SURFACE)
        {
            m_sEGL_Base_Conf->m_bSuccess = EGL_FALSE;
        }
    }
    //eglBindTexImage(m_sEGL_Base_Conf->m_vEglDisplay, m_sEGL_Base_Conf->m_vEglSurface, 1);
    
}

/**
 *
 */
void EGL_Base::initPBufferSurface()
{
    EGLint PBufAttribs[] =  
    {  
	EGL_WIDTH,  320,  
	EGL_HEIGHT, 480,  
	EGL_LARGEST_PBUFFER, EGL_TRUE,  
	EGL_NONE  
    };  
    m_sEGL_Base_Conf->m_vEglSurface = eglCreatePbufferSurface(m_sEGL_Base_Conf->m_vEglDisplay, m_sEGL_Base_Conf->m_vEglConfig, PBufAttribs);
    
}

/**
 *
 */
void EGL_Base::makeCurrent()
{
    if (m_sEGL_Base_Conf->m_bSuccess != EGL_FALSE)
    {
        m_sEGL_Base_Conf->m_bSuccess = eglMakeCurrent(m_sEGL_Base_Conf->m_vEglDisplay, m_sEGL_Base_Conf->m_vEglSurface,
                                 m_sEGL_Base_Conf->m_vEglSurface, m_sEGL_Base_Conf->m_vEglContext);
    }
    
    if (!eglQuerySurface(m_sEGL_Base_Conf->m_vEglDisplay, m_sEGL_Base_Conf->m_vEglSurface, EGL_HEIGHT, &m_sEGL_Base_Conf->m_nScreenHeight)  
	|| !eglQuerySurface(m_sEGL_Base_Conf->m_vEglDisplay, m_sEGL_Base_Conf->m_vEglSurface, EGL_WIDTH, &m_sEGL_Base_Conf->m_nScreenWidth) )
    {  
	// 查询不到信息，报错  
    }
    
    LOGI("width height %d %d \n", m_sEGL_Base_Conf->m_nScreenWidth, m_sEGL_Base_Conf->m_nScreenHeight);
    
}

/**
 *
 */
int EGL_Base::Release()
{
    if (m_sEGL_Base_Conf->m_vEglDisplay != EGL_NO_DISPLAY)
    {
	eglMakeCurrent(m_sEGL_Base_Conf->m_vEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    }
    if (m_sEGL_Base_Conf->m_vEglContext != EGL_NO_CONTEXT)
    {
	eglDestroyContext(m_sEGL_Base_Conf->m_vEglDisplay, m_sEGL_Base_Conf->m_vEglContext);
    }
    if(m_sEGL_Base_Conf->m_vEglSurface != EGL_NO_SURFACE)
    {
	eglDestroySurface(m_sEGL_Base_Conf->m_vEglDisplay, m_sEGL_Base_Conf->m_vEglSurface);
    }
    eglTerminate(m_sEGL_Base_Conf->m_vEglDisplay);
    
    m_sEGL_Base_Conf->m_vEglDisplay = EGL_NO_DISPLAY;
    m_sEGL_Base_Conf->m_vEglContext = EGL_NO_CONTEXT;
    m_sEGL_Base_Conf->m_vEglSurface = EGL_NO_SURFACE;
    
    return 1;
}

/**
 *
 */
void EGL_Base::swapBuffer()
{
    eglSwapBuffers(m_sEGL_Base_Conf->m_vEglDisplay, m_sEGL_Base_Conf->m_vEglSurface);
}














